package com.rahms.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rahms.pojo.entity.HealthData;
import com.rahms.server.mapper.HealthDataMapper;
import com.rahms.server.service.HealthDataService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 健康数据服务实现类
 */
@Service
public class HealthDataServiceImpl extends ServiceImpl<HealthDataMapper, HealthData> implements HealthDataService {
    
    @Override
    public List<HealthData> getHealthDataByElderlyIdAndType(Long elderlyId, String dataType) {
        LambdaQueryWrapper<HealthData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HealthData::getElderlyId, elderlyId)
                   .eq(HealthData::getDataType, dataType)
                   .orderByDesc(HealthData::getRecordTime);
        return this.list(queryWrapper);
    }
    
    @Override
    public List<HealthData> getHealthDataByDateRange(Long elderlyId, String dataType, LocalDateTime startDate, LocalDateTime endDate) {
        LambdaQueryWrapper<HealthData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HealthData::getElderlyId, elderlyId)
                   .eq(dataType != null && !dataType.isEmpty(), HealthData::getDataType, dataType)
                   .between(HealthData::getRecordTime, startDate, endDate)
                   .orderByDesc(HealthData::getRecordTime);
        return this.list(queryWrapper);
    }
    
    @Override
    public Map<String, Object> getHealthDataStatistics(Long elderlyId, String dataType, LocalDateTime startDate, LocalDateTime endDate) {
        List<HealthData> dataList = getHealthDataByDateRange(elderlyId, dataType, startDate, endDate);
        Map<String, Object> statistics = new HashMap<>();
        
        if (dataList.isEmpty()) {
            statistics.put("count", 0);
            statistics.put("average", null);
            statistics.put("max", null);
            statistics.put("min", null);
            return statistics;
        }
        
        statistics.put("count", dataList.size());
        
        // 根据数据类型计算统计值
        if ("blood_pressure".equals(dataType)) {
            // 处理血压数据 "120/80"
            double sumSystolic = 0, sumDiastolic = 0;
            double maxSystolic = Double.MIN_VALUE, maxDiastolic = Double.MIN_VALUE;
            double minSystolic = Double.MAX_VALUE, minDiastolic = Double.MAX_VALUE;
            
            for (HealthData data : dataList) {
                try {
                    String[] values = data.getDataValue().split("/");
                    double systolic = Double.parseDouble(values[0]);
                    double diastolic = Double.parseDouble(values[1]);
                    
                    sumSystolic += systolic;
                    sumDiastolic += diastolic;
                    
                    maxSystolic = Math.max(maxSystolic, systolic);
                    maxDiastolic = Math.max(maxDiastolic, diastolic);
                    
                    minSystolic = Math.min(minSystolic, systolic);
                    minDiastolic = Math.min(minDiastolic, diastolic);
                } catch (Exception e) {
                    // 忽略格式错误的数据
                }
            }
            
            statistics.put("averageSystolic", sumSystolic / dataList.size());
            statistics.put("averageDiastolic", sumDiastolic / dataList.size());
            statistics.put("maxSystolic", maxSystolic);
            statistics.put("maxDiastolic", maxDiastolic);
            statistics.put("minSystolic", minSystolic);
            statistics.put("minDiastolic", minDiastolic);
        } else {
            // 处理其他类型的数值数据
            double sum = 0;
            double max = Double.MIN_VALUE;
            double min = Double.MAX_VALUE;
            
            for (HealthData data : dataList) {
                try {
                    double value = Double.parseDouble(data.getDataValue());
                    sum += value;
                    max = Math.max(max, value);
                    min = Math.min(min, value);
                } catch (Exception e) {
                    // 忽略格式错误的数据
                }
            }
            
            statistics.put("average", sum / dataList.size());
            statistics.put("max", max);
            statistics.put("min", min);
        }
        
        return statistics;
    }
}